package com.ywx.yypicturebackend.manager;

import cn.hutool.core.io.FileUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.*;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import com.ywx.yypicturebackend.config.CosClientConfig;
import com.ywx.yypicturebackend.constant.FileConstant;
import com.ywx.yypicturebackend.model.entity.Picture;
import lombok.Data;
import org.checkerframework.checker.units.qual.K;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 与业务无关的Cos操作方法
 */
@Component
@Data
public class CosManager {

    @Resource
    CosClientConfig cosClientConfig;

    @Resource
    COSClient cosClient;


    /**
     * 上传对象
     *
     * @param key  唯一键
     * @param file 文件
     *             PutObjectResult 上传结果
     */
    public PutObjectResult putObject(String key, File file) {

        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key,
                file);
        return cosClient.putObject(putObjectRequest);
    }


    /**
     * 下载对象
     *
     * @param key 唯一键
     *            COSObject 下载结果
     */
    public COSObject getObject(String key) {
        GetObjectRequest getObjectRequest = new GetObjectRequest(cosClientConfig.getBucket(), key);
        return cosClient.getObject(getObjectRequest);
    }


    /**
     * 上传对象（附带图片信息）
     *
     * @param key  唯一键
     * @param file 文件
     */
    public PutObjectResult putPictureObject(String key, File file) {

        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key,
                file);

        // 对图片进行处理（获取基本信息也被视作为一种处理）
        PicOperations picOperations = new PicOperations();
        // 1 表示返回原图信息
        picOperations.setIsPicInfo(1);
        List<PicOperations.Rule> rules = new ArrayList<>();
        //图片格式转换(转成webp格式)
        String webpKey = FileUtil.mainName(key) + FileConstant.PIC_WEBP_SUFFIX;
        PicOperations.Rule compressRule = new PicOperations.Rule();
        //添加图片格式转换规则,以压缩图片
        compressRule.setRule("imageMogr2/format/webp");//这样COS会存储一份原图和一份格式围殴Webp的图片
        compressRule.setBucket(cosClientConfig.getBucket());
        compressRule.setFileId(webpKey);
        rules.add(compressRule);

        //如果文件大小大于20kb 进行图片缩略,否则可能缩略图的大会比压缩图大
        if (file.length() > 128 * 20) {
            PicOperations.Rule thumbnailRule = new PicOperations.Rule();
            String thumbnailKey = FileUtil.mainName(key) + "_thumbnail." + FileUtil.getSuffix(key);
            //添加图片缩略规则
            thumbnailRule.setRule(String.format("imageMogr2/thumbnail/%sx%s>", 256, 256));
            thumbnailRule.setFileId(thumbnailKey);
            thumbnailRule.setBucket(cosClientConfig.getBucket());
            rules.add(thumbnailRule);
        }
        // 构造处理参数,设置图片处理
        picOperations.setRules(rules);
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }


    /**
     * 删除数据
     *
     * @param key 数据的唯一键
     */
    public void deleteObject(String key) {
        cosClient.deleteObject(cosClientConfig.getBucket(), key);
    }

    public void deleteObjectByKeys(ArrayList<String> keys) {
        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(cosClientConfig.getBucket());
        ArrayList<DeleteObjectsRequest.KeyVersion> keyList = null;
        keyList = (ArrayList<DeleteObjectsRequest.KeyVersion>) keys.stream().map(key -> new DeleteObjectsRequest.KeyVersion(key)).collect(Collectors.toList());

        deleteObjectsRequest.setKeys(keyList);
        cosClient.deleteObjects(deleteObjectsRequest);
    }





}

